home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-07-28 | 12.3 KB | 596 lines | [TEXT/MPS ] |
- /*
- File: DiskEntry.cp
-
- Copyright: © 1991-1994 by Apple Computer, Inc.
- All rights reserved.
-
- Part of the AOCE Sample SMSAM Package. Consult the license
- which came with this software for your specific legal rights.
-
- */
-
-
-
- #ifndef __DISKENTRY__
- #include "DiskEntry.h"
- #endif
-
- #ifndef __ABSTRACTFILE__
- #include "AbstractFile.h"
- #endif
-
- #ifndef __STDIO__
- #include "StdIO.h"
- #endif
-
- #ifndef __STRING__
- #include "String.h"
- #endif
-
- #ifndef __IOSTREAM__
- #include "IOStream.h"
- #endif
-
- #ifndef __STDLIB__
- #include "StdLib.h"
- #endif
-
- #pragma segment DiskLog
-
- /***********************************|****************************************/
-
- const EntryID TLogEntry::kInvalidID = (EntryID) 0; // should be zero
- const unsigned long TTestEntry::kChunkSize = 64;
- const char* const kSampleData = "all work and no play makes keith a dull boy - ";
-
- /***********************************|****************************************/
- /***********************************|****************************************/
-
- TLogEntry::TLogEntry ():
- fID ( kInvalidID ),
- fTime (),
- fCache ( nil )
- {
- fTime.SetToLocalTime ();
- }
-
- /***********************************|****************************************/
-
- TLogEntry::TLogEntry ( const TLogEntry& that ):
- fID ( that.fID ),
- fTime ( that.fTime ),
- fCache ( nil )
- {
- }
-
- /***********************************|****************************************/
-
- TLogEntry&
- TLogEntry::operator = ( const TLogEntry& that )
- {
- if ( this != &that )
- {
- fID = that.fID;
- fTime = that.fTime;
- delete fCache;
- fCache = nil;
- }
-
- return *this;
- }
-
- /***********************************|****************************************/
-
- Boolean
- TLogEntry::operator == ( const TLogEntry& that ) const
- {
- return ( fID == that.fID ) && ( fTime == that.fTime );
- }
-
- /***********************************|****************************************/
-
- Boolean
- TLogEntry::operator != ( const TLogEntry& that ) const
- {
- return !operator == ( that );
- }
-
- /***********************************|****************************************/
-
- unsigned long
- TLogEntry::GetTextLength () const
- {
- if ( !fCache )
- ( (TLogEntry*) this )->fCache = CreateText ();
-
- return strlen ( fCache );
- }
-
- /***********************************|****************************************/
-
- void
- TLogEntry::GetText ( char* buffer, unsigned long size ) const
- {
- if ( !fCache )
- ( (TLogEntry*) this )->fCache = CreateText ();
-
- strncpy ( buffer, fCache, (int) size );
- }
-
- /***********************************|****************************************/
-
- char*
- TLogEntry::CreateText () const
- { char buffer [ 256 ];
-
- const char* kFormat = "LogEntry %li (%s)\n";
- char* timeString = fTime.CreateString ();
- unsigned long length = sprintf ( buffer, kFormat, fID, timeString );
- char* finalString = new char [ length + 1 ];
- if ( finalString )
- {
- memcpy ( finalString, buffer, length + 1 );
- }
- delete timeString;
-
- return finalString;
- }
-
- /***********************************|****************************************/
-
- Boolean
- TLogEntry::WriteTo ( TAbstractFile& file ) const
- {
- if ( file.WriteDataIgnore ( &( (TLogEntry*) this )->fID, sizeof ( fID ) ) != noErr )
- return false;
- return fTime.WriteTo ( file );
- }
-
- /***********************************|****************************************/
-
- Boolean
- TLogEntry::ReadFrom ( TAbstractFile& file )
- {
- if ( file.ReadDataIgnore ( &fID, sizeof ( fID ) ) != noErr )
- return false;
- return fTime.ReadFrom ( file );
- }
-
- /***********************************|****************************************/
-
- const char*
- TLogEntry::GetClass () const
- {
- return "TLogEntry";
- }
-
- /***********************************|****************************************/
-
- ostream&
- TLogEntry::operator >> ( ostream& stream ) const
- {
- stream << GetClass () << ":" << (void*) this << "\n";
- stream << "\tfID:" << fID << "\n\tfTime:";
- fTime >> stream;
- stream.flush ();
- return stream;
- }
-
- /***********************************|****************************************/
- /***********************************|****************************************/
-
- #pragma trace off
-
- TTestEntry::TTestEntry ():
- TLogEntry (),
- fLength ( RandomLength () )
- {
- }
-
- /***********************************|****************************************/
-
- TTestEntry::TTestEntry ( unsigned long length ):
- TLogEntry (),
- fLength ( length )
- {
- }
-
- /***********************************|****************************************/
-
- TTestEntry::~TTestEntry ()
- {
- }
-
- /***********************************|****************************************/
-
- TTestEntry::TTestEntry ( const TTestEntry& that ):
- TLogEntry ( that ),
- fLength ( that.fLength )
- {
- }
-
- /***********************************|****************************************/
-
- TTestEntry&
- TTestEntry::operator = ( const TTestEntry& that )
- {
- if ( this != &that )
- {
- TLogEntry::operator = ( that );
- fLength = that.fLength;
- }
-
- return *this;
- }
-
- /***********************************|****************************************/
-
- const char*
- TTestEntry::GetClass () const
- {
- return "TTestEntry";
- }
-
- /***********************************|****************************************/
-
- Boolean
- TTestEntry::WriteTo ( TAbstractFile& file ) const
- {
- if ( !TLogEntry::WriteTo ( file ) )
- return false;
-
- if ( file.WriteDataIgnore ( &( (TTestEntry*) this )->fLength, sizeof ( fLength ) ) != noErr )
- return false;
-
- unsigned long remaining = fLength, nextWrite;
- char buffer [ kChunkSize ];
- FillBuffer ( buffer, sizeof ( buffer ) );
-
- while ( remaining > 0 )
- {
- nextWrite = remaining > kChunkSize ? kChunkSize : remaining;
- remaining -= nextWrite;
-
- if ( file.WriteDataIgnore ( buffer, nextWrite ) != noErr )
- return false;
- }
-
- return true;
- }
-
- /***********************************|****************************************/
-
- Boolean
- TTestEntry::ReadFrom ( TAbstractFile& file )
- {
- if ( !TLogEntry::ReadFrom ( file ) )
- return false;
-
- if ( file.ReadDataIgnore ( &fLength, sizeof ( fLength ) ) != noErr )
- return false;
-
- unsigned long remaining = fLength, nextRead;
- char buffer [ kChunkSize ];
-
- while ( remaining > 0 )
- {
- nextRead = remaining > kChunkSize ? kChunkSize : remaining;
- remaining -= nextRead;
-
- if ( file.ReadDataIgnore ( buffer, nextRead ) != noErr )
- return false;
-
- if ( !VerifyBuffer ( buffer, nextRead ) )
- return false;
- }
-
- return true;
- }
-
- /***********************************|****************************************/
-
- inline char
- BufferChar ( unsigned long index )
- {
- return '='; // 'A' + ( ( 'z' - 'A' ) % (char) ( index + 1 ) ) - 1;
- }
-
- /***********************************|****************************************/
-
- void
- TTestEntry::FillBuffer ( char* buffer, unsigned long length )
- {
- for ( unsigned long i = 0; i < length; i++ )
- *buffer++ = BufferChar ( i );
- }
-
- /***********************************|****************************************/
-
- Boolean
- TTestEntry::VerifyBuffer ( char* buffer, unsigned long length )
- {
- for ( unsigned long i = 0; i < length; i++ )
- if ( *buffer++ != BufferChar ( i ) )
- return false;
-
- return true;
- }
-
- /***********************************|****************************************/
-
- ostream&
- TTestEntry::operator >> ( ostream& stream ) const
- {
- TLogEntry::operator >> ( stream );
- stream << "\tfLength:" << fLength << "\n";
- return stream;
- }
-
- /***********************************|****************************************/
-
- unsigned long
- TTestEntry::RandomLength ( unsigned long min, unsigned long max )
- {
- return min + ( rand() % ( max - min ) );
- }
-
- /***********************************|****************************************/
-
- Boolean
- TTestEntry::operator == ( const TTestEntry& that ) const
- {
- return
- TLogEntry::operator == ( (TLogEntry&) (TTestEntry&) that ) &&
- ( fLength == that.fLength );
- }
-
- /***********************************|****************************************/
-
- Boolean
- TTestEntry::operator != ( const TTestEntry& that ) const
- {
- return !operator == ( that );
- }
-
- /***********************************|****************************************/
- /***********************************|****************************************/
-
- TErrorEntry::TErrorEntry ():
- TLogEntry (),
- fFile ( nil ),
- fLine ( 0 ),
- fMessage ( nil )
- {
- }
-
- /***********************************|****************************************/
-
- TErrorEntry::TErrorEntry ( const TErrorEntry& that ):
- TLogEntry ( that ),
- fFile ( nil ),
- fLine ( 0 ),
- fMessage ( nil )
- {
- if ( that.fFile )
- {
- fFile = new char [ strlen ( that.fFile ) + 1 ];
- strcpy ( fFile, that.fFile );
- }
-
- fLine = that.fLine;
-
- if ( that.fMessage )
- {
- fMessage = new char [ strlen ( that.fMessage ) + 1 ];
- strcpy ( fMessage, that.fMessage );
- }
- }
-
- /***********************************|****************************************/
-
- TErrorEntry&
- TErrorEntry::operator = ( const TErrorEntry& that )
- {
- if ( this != &that )
- {
- TLogEntry::operator = ( that );
-
- delete fFile;
- if ( that.fFile )
- {
- fFile = new char [ strlen ( that.fFile ) + 1 ];
- strcpy ( fFile, that.fFile );
- }
- else
- {
- fFile = nil;
- }
-
- fLine = that.fLine;
-
- delete fMessage;
- if ( that.fMessage )
- {
- fMessage = new char [ strlen ( that.fMessage ) + 1 ];
- strcpy ( fMessage, that.fMessage );
- }
- else
- {
- fMessage = nil;
- }
- }
-
- return *this;
- }
-
- /***********************************|****************************************/
-
- TErrorEntry::~TErrorEntry ()
- {
- delete fFile;
- delete fMessage;
- }
-
- /***********************************|****************************************/
-
- unsigned long
- TErrorEntry::GetString ( char* buffer ) const
- {
- if ( fFile && fMessage )
- {
- return sprintf ( buffer, "FILE \"%s\"; LINE %li; # %s\n", fFile, fLine, fMessage );
- }
- else if ( fFile )
- {
- return sprintf ( buffer, "FILE \"%s\"; LINE %li\n", fFile, fLine );
- }
- else if ( fMessage )
- {
- return sprintf ( buffer, "# %s\n", fMessage );
- }
- else
- {
- return sprintf ( buffer, "# Unknown error.\n" );
- }
- }
-
- /***********************************|****************************************/
-
- unsigned long
- TErrorEntry::GetTextLength () const
- {
- return GetString ( nil );
- }
-
- /***********************************|****************************************/
-
- void
- TErrorEntry::GetText ( char* buffer, unsigned long size ) const
- {
- char* string = CreateText ();
- strncpy ( buffer, string, (unsigned int) size );
- delete string;
- }
-
- /***********************************|****************************************/
-
- char*
- TErrorEntry::CreateText () const
- {
- char* string = new char [ GetString ( nil ) + 1 ];
- GetString ( string );
- return string;
- }
-
- /***********************************|****************************************/
-
- Boolean
- TErrorEntry::WriteTo ( TAbstractFile& file ) const
- {
- if ( !Write ( fFile, file ) )
- return false;
-
- if ( file.WriteDataIgnore ( &( (TErrorEntry*) this )->fLine, sizeof ( fLine ) ) != noErr )
- return false;
-
- if ( !Write ( fMessage, file ) )
- return false;
-
- return true;
- }
-
- /***********************************|****************************************/
-
- Boolean
- TErrorEntry::ReadFrom ( TAbstractFile& file )
- {
- delete fFile;
-
- if ( !Read ( fFile, file ) )
- return false;
-
- if ( file.ReadDataIgnore ( &fLine, sizeof ( fLine ) ) != noErr )
- return false;
-
- delete fMessage;
-
- if ( !Read ( fMessage, file ) )
- return false;
-
- return true;
- }
-
- /***********************************|****************************************/
-
- Boolean
- TErrorEntry::Write ( const char* string, TAbstractFile& file ) const
- {
- unsigned long length = 0;
-
- if ( string )
- length = strlen ( string );
-
- if ( file.Write ( length ) != noErr )
- return false;
-
- if ( string )
- if ( file.WriteDataIgnore ( string, length ) != noErr )
- return false;
-
- return true;
- }
-
- /***********************************|****************************************/
-
- Boolean
- TErrorEntry::Read ( char*& string, TAbstractFile& file ) const
- {
- string = nil;
- unsigned long length = 0;
-
- if ( file.Read ( length ) != noErr )
- return false;
-
- if ( length > 0 )
- {
- string = new char [ length + 1 ];
-
- if ( string )
- {
- if ( file.ReadDataIgnore ( string, length ) != noErr )
- {
- delete string;
- string = nil;
- return false;
- }
-
- string [ length ] = 0; // add null terminator
- }
- else
- {
- file.SetPosition ( fsFromMark, length ); // skip over string data
- return false;
- }
- }
-
- return true;
- }
-
- /***********************************|****************************************/
-
- const char*
- TErrorEntry::GetClass () const
- {
- return "TErrorEntry";
- }
-
- /***********************************|****************************************/
-
- ostream&
- TErrorEntry::operator >> ( ostream& stream ) const
- {
- return stream;
- }
-
- /***********************************|****************************************/
-